package com.service;

import com.dao.IBisDataDao;
import com.dao.IRetPrdInfoDao;
import com.dao.IRetWODao;
import com.icim.service.ICIMBaseService;
import com.model.Bis_data;
import com.model.Ret_wo;
import com.tx.Xpchkoos.XpchkoosI;
import com.tx.Xpchkoos.XpchkoosO;
import com.tx.Xpchkoos.XpchkoosOA;
import com.util.MqOpe;
import com.util.Util;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

@Scope("prototype")
@Service("XpchkoosService")
public class XpchkoosService extends ICIMBaseService {
	private static final Logger logger = Logger.getLogger(XpchkoosService.class);
	@Autowired
	XpchkoosI inTrx;
	@Autowired
	XpchkoosO outTrx;

	@Autowired
    IRetPrdInfoDao retPrdInfoDao;

	@Autowired
    IRetWODao retWoDao;

	/*@Autowired
	IBisRecipeDao bisRecipeDao;*/

	@Autowired
	private IBisDataDao bisDataDao;

	private final long _NORMAL = 0;
	private final long E_NOT_FOUND_SHT = 1;
	private final long E_NOT_FOUND_WO = 2;
	private String gszMsgBuf = "";

	public String subMainProc(String strInMsg) {
		MqOpe mqOpe = new MqOpe();
		String reply = "";
		outTrx.setTrx_id("XPCMSRDT");
		outTrx.setType_id("O");
		try {
			logger.info("InTrx is " + strInMsg);
			inTrx = (XpchkoosI) mqOpe.inStr2Obj(strInMsg, inTrx);
			long lRc = SubMainProc2();
			if (lRc != _NORMAL) {
				outTrx.setRtn_code("000000" + lRc);
				outTrx.setRtn_mesg(gszMsgBuf);
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			} else {
				outTrx.setRtn_code("0000000");
				outTrx.setRtn_mesg("success");
			}
		} catch (Exception ex) {
			outTrx.setRtn_code("0000001");
			outTrx.setRtn_mesg(ex.getMessage());
			logger.error(Util.stackTraceToString(ex));
			ex.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return mqOpe.obj2Str(outTrx);
		} finally {
			reply = mqOpe.obj2Str(outTrx);
			logger.info(reply);
		}
		return reply;

	}

	public long SubMainProc2() throws IllegalAccessException, InvocationTargetException {
		long lRc;

		String actionFlg = inTrx.getAction_flg();

		return checkSpcSpecRuleProc();

	}

	public long checkSpcSpecRuleProc() {

		String woId = inTrx.getWo_id();
		String pathId = inTrx.getPath_id();
		String opeId = inTrx.getOpe_id();
		String mdlId = inTrx.getMdl_id();
		String mesId = inTrx.getMes_id();

		Ret_wo retWo = retWoDao.get(woId);
		String cusId = retWo.getCus_id_fk();

		String specCate = null;
		Double thickness = null;

		List<XpchkoosOA> oaryList = new ArrayList<XpchkoosOA>();
		
		
		if ("CDPC".equals(mesId)) {
			if (inTrx.getThickness_type().equals("T")) {
				specCate = "CD_T";
				thickness = retWo.getTo_thickness();
			} else if (inTrx.getThickness_type().equals("S")) {
				specCate = "CD_S";
				thickness = retWo.getFrom_thickness();
			}
		} else if ("CZPC".equals(mesId)) {
			specCate = "CZ_C";
		}

		if (!StringUtils.isEmpty(specCate)) {
			Bis_data rule = bisDataDao.getRuleProc(specCate, cusId, mdlId, woId, pathId, opeId);
			if (rule != null) {
				logger.info(rule);

				Double usl = null, lsl = null;

				String ruleCate = rule.getData_cate();

				if (thickness != null && ruleCate.equals("CMRU")) {
					usl = Double.valueOf(rule.getData_ext()) + thickness;
					lsl = thickness - Double.valueOf(rule.getData_item());
				} else if (ruleCate.equals("SPRU")) {
					usl = Double.valueOf(rule.getData_ext());
					lsl = Double.valueOf(rule.getData_item());
				} else {
					gszMsgBuf = "当前规则设置有误:" + rule.toString();
					return 1;
				}

				logger.info("usl:" + usl);
				logger.info("lsl:" + lsl);

				StringBuffer buffer = new StringBuffer(ruleCate.equals("SPRU") ? "特殊规则:" : "普通规则 \n\r ");

				String cate = rule.getData_desc();
				String cateChin = null;
				if (cate.equals("CD_S")) {
					cateChin = "来料厚度";
				} else if (cate.equals("CD_T")) {
					cateChin = "目标厚度";
				} else if (cate.equals("CZ_C")) {
					cateChin = "普通电阻";
				}
				String rCusId = rule.getExt_1();
				String rMdlId = rule.getExt_2();
				String rWoId = rule.getExt_3();
				String rRouteId = rule.getExt_4();
				String rOpeId = rule.getExt_5();
				
				buffer.append("类型:[").append(cateChin).append("] \n");
				buffer.append("客户:[").append(rCusId).append("] \n");
				buffer.append("型号:[").append(rMdlId).append("] \n");
				buffer.append("订单:[").append(rWoId).append("] \n");
				buffer.append("工艺:[").append(rRouteId).append("] \n");
				buffer.append("站点:[").append(rOpeId).append("] \n");
				
				
				if (ruleCate.equals("SPRU")) {
					buffer.append("上限:[").append(String.format("%.3f", usl)).append("] \n");
					buffer.append("下限:[").append(String.format("%.3f", lsl)).append("] \n");
				} else {
					buffer.append("上限误差:[").append(String.format("%.3f", Double.valueOf(rule.getData_ext()))).append("] \n");
					buffer.append("下限误差:[").append(String.format("%.3f", Double.valueOf(rule.getData_item()))).append("] \n");
					if (thickness != null) {
						buffer.append("订单厚度:[").append(thickness).append("]");
					}
				}
				
				XpchkoosOA oary = new XpchkoosOA();
				oary.setRule_cate(cate);
				oary.setCus_id(rCusId);
				oary.setMdl_id(rMdlId);
				oary.setWo_id(rWoId);
				oary.setRoute_id(rRouteId);
				oary.setOpe_id(rOpeId);
				oary.setUsl(usl);
				oary.setLsl(lsl);
				oary.setAlarm_message(buffer.toString());
				oaryList.add(oary);
			}
		}
		outTrx.setRtn_code("0000000");
		outTrx.setRtn_mesg("success");
		outTrx.setOary(oaryList);
		outTrx.setTbl_cnt(oaryList.size());

		return 0;

	}

}
